local shexian = fk.CreateSkill {
  name = "th_shexian",
  max_phase_use_time = 1,
}

shexian:addEffect("active", {
  name = "th_shexian",
  anim_type = "control",
  prompt = "#th_shexian",
  card_num = 0,
  target_num = 1,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local target = effect.tos[1]
    local judge = {
      who = target,
      reason = shexian.name,
      pattern = ".",
      skipDrop = true,
    }
    room:judge(judge)
    -- if not target.dead and judge.card and room:getCardArea(judge.card) == Card.Processing then
    --   target:addToPile("th_shexian", judge.card, true, th_shexian.name, player.id, table.map(room.alive_players, Util.IdMapper))
    --   room:setPlayerMark(target, "_th_shexian", player.id)
    -- end
  end
}, { check_skill_limit = true })

shexian:addEffect(fk.FinishJudge, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and not player.dead and data.reason == shexian.name
      and player.room:getCardArea(data.card.id) == Card.Processing
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player:addToPile("th_shexian", data.card, true, shexian.name)
  end,
})

shexian:addEffect(fk.CardUseFinished, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(shexian.name) and target then
      return #target:getPile("th_shexian") ~= 0 and table.every(player.room:getAlivePlayers(false), function(p) return not p.dying end)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(target:getPile("th_shexian"), function(cid)
      local card = Fk:getCardById(cid)
      return card and card.suit == data.card.suit
    end)
    if #cards == 0 then return end
    if room:askToSkillInvoke(player, {
      skill_name = shexian.name,
      prompt = "#th_shexian-invoke::" .. target.id .. ":" .. data.card:getSuitString()
    }) then
      event:setCostData(self, { cards = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = room:askToChooseCard(player, {
      target = target,
      flag = { card_data = { { "th_shexian", event:getCostData(self).cards }  } },
      skill_name = shexian.name
    })
    room:obtainCard(player, card, false, fk.ReasonPrey)
    room:damage{
      from = player,
      to = target,
      damage = 1,
      skillName = "th_shexian",
    }
  end
})

Fk:loadTranslationTable{
  ["th_shexian"] = "设陷",
  [":th_shexian"] = "出牌阶段限一次，你可以令一名其他角色判定并将判定牌置于其武将牌上，其使用牌后，若没有角色处于濒死状态，你可以获得其武将牌上一张同花色“设陷”牌并对其造成1点伤害。",

  ["#th_shexian"] = "设陷：令一名其他角色判定，其使用与此牌同花色的牌后，你可以对其造成1点伤害",

  ["#th_shexian-invoke"] = "设陷：你可以获得 %dest 武将牌上的一张 %arg “设陷”牌并对其造成1点伤害",
}

return shexian